/*
 * @Date: 2023-03-09 14:03:09
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-10 19:56:22
 * @FilePath: \A19\backend\src\api\Get\ApiGetTestQuestions.ts
 */
import { ApiCall } from "tsrpc";
import { ReqGetTestQuestions, ResGetTestQuestions } from "../../shared/protocols/Get/PtlGetTestQuestions";
import { Global } from '../../Global/index';
import { ObjectId } from "mongodb";
import { questions } from "../../shared/types/Question";

export default async function (call: ApiCall<ReqGetTestQuestions, ResGetTestQuestions>) {
    // TODO
    const testId= call.req.test
    const examOrHomework = call.req.examOrHomework
    const ret = await Global.collection(examOrHomework === 'exam'?'tests':'homework').aggregate([
        {
            $match: {
                _id:testId
            }
        },
        {
            $lookup: {
                from: 'questions',
                localField: 'questions',
                foreignField: '_id',
                as: 'questions'
            }
        }
    ]).toArray()

    call.succ({
        test:ret as Array<{
            _id: ObjectId,
            course: ObjectId,
            startTime: Date,
            endTime: Date,
            name: string,
            status: "saved" | "published"
            questions:Array<questions>
           }>
    })
}